<template>
  <div>
    <el-card shadow="always">
      <el-form ref="management" :inline="true" size="small" style="display:flex;justify-content: space-between">
        <el-form-item>
          <el-input placeholder="请输入用户名" v-model="queryParams.acount" style="width: 300px;">
            <el-button slot="append" icon="el-icon-search" @click="getlist"></el-button>
            <el-button slot="append" type="primary" icon="el-icon-refresh" round
                       @click="selectlist"></el-button>
          </el-input>

        </el-form-item>
        <!--            <el-form-item>-->
        <!--                <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增订阅策略</el-button>-->
        <!--            </el-form-item>-->


      </el-form>
      <el-table stripe
                :data="tableData1"
                style="width: 100%">
        <el-table-column prop="acount" label="用户名"></el-table-column>
        <el-table-column prop="sname" label="能力名称"></el-table-column>
        <el-table-column prop="level" label="当前节流策略"></el-table-column>

        <el-table-column align="center" label="操作">
          <!-- <template slot="header" slot-scope="scope">
              <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
          </template> -->
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-row>
        <el-col style="text-align:right">
          <pagination
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getlist"
          />

        </el-col>
      </el-row>
      <!-- <div class="block">
          <el-pagination v-show="total > 0" @size-change="handleSizeChange" @current-change="handleCurrentChange"
              :page-sizes="[2, 3, 4]" layout="total, sizes, prev, pager, next, jumper" :total="total" background
              :current-page.sync="queryParams.currentPage" :page-size.sync="queryParams.pageSize" />
      </div> -->
      <!-- 修改 -->
      <el-dialog title="修改" :visible.sync="openEdit" width="300px" >
        <el-form :model="form" ref="form" label-position="center">

          <el-form-item label="当前节流策略"  :label-width="formLabelWidth">
            <el-radio-group v-model="form.level">
              <el-radio  v-for="item in levellist"  :label="Number(item )">{{item}}</el-radio> <br>
            </el-radio-group>

            <!-- <el-radio v-model="level" label="2000">2000</el-radio>
            <el-radio v-model="level" label="3000">3000</el-radio> -->
            <!-- <el-checkbox-group v-model="level">

                <el-checkbox label="100" checked></el-checkbox>
                <el-checkbox label="200"></el-checkbox>
                <el-checkbox label="300"></el-checkbox>
            </el-checkbox-group> -->
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer" >
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getsublistFn, selectsublistFn, updatesublistFn, getlevellistFn } from "@/api/sublist.js"
export default {
  name: 'orgManagement',
  data() {
    return {
      checklist: [],
      tableData1: [],
      keyWord: '',
      search: '',
      acount: '',
      total: 1,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        acount:null
      },
      levellist: [],
      openAdd: false,//新增对话框,
      openEdit: false,//修改对话框
      openDetail: false,//详情对话框
      form: {

      },
      formLabelWidth: '160px',
      rules: {
        userName: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        orgName: [
          { required: true, message: "机构名称不能为空", trigger: "blur" }
        ],
        capId: [
          { required: true, message: "能力id不能为空", trigger: "blur" }
        ],
        capName: [
          { required: true, message: "能力名称不能为空", trigger: "blur" }
        ],
        throttle: [
          { required: true, message: "当前节流策略不能为空", trigger: "blur" }
        ],
      }
    }
  },
  methods: {

    cancel() {

      this.openAdd = false
      this.openEdit = false
    },
    selectlist() {
      this.loading = true
      this.queryParams.acount=null
      this.getlist()
    },
    getlist() {
      this.loading = true
      getsublistFn(this.queryParams).then(res => {
        this.tableData1 = res.rows
        this.total = res.total
      })
    },
    handleAdd() {
      this.$refs['form']?.clearValidate()
      this.reset()
      this.openAdd = true
    },
    handleEdit(index, row) {

      getlevellistFn(row.sid).then(res => {
        this.levellist = res.data[0].levelgroup
        console.log(this.levellist)
      })
      this.form = { ...row }
      this.form.level = row.level
      console.log(this.form)

      this.openEdit = true
    },
    detail(index, row) {
      this.form =row
      this.openDetail = true
    },
    // submitForm() {
    //     this.$refs['form'].validate((valid) => {
    //         if (valid) {
    //             alert('submit!');
    //             this.openAdd = false
    //             this.openEdit = false
    //             this.reset()
    //         } else {
    //             console.log('error submit!!');
    //             return false;
    //         }
    //     });
    // },
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {

          updatesublistFn(this.form).then(res => {
            this.$modal.msgSuccess('修改成功');
            this.getlist()
            this.openEdit = false
          }).catch((error) => {
            console.error(error);
          });

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    handleSizeChange(val) {
      this.queryParams.currentPage = 1
      this.queryParams.pageSize = val
    },
    handleCurrentChange(val) {
      this.queryParams.currentPage = val
    }
  },
  created() {
    this.getlist()
  }
}
</script>
<style scoped lang="scss">
/* 卡片样式 */
.el-card {
  width: 98%;
  margin: 10px auto;
}

.header {
  height: 80px;
  line-height: 80px;
  position: relative;
}


.header .el-button {
  margin-left: 80%;
}
.dialog-footer{
  margin-right:20%;
  margin-top:100px;
}

/* 分页器样式 */
.block {
  margin: 20px 0 0 60%;
}

</style>
